package cn.iocoder.yudao.module.jifenyuan.service.wallet;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.jifenyuan.controller.app.wallet.vo.*;
import cn.iocoder.yudao.module.jifenyuan.dal.dataobject.wallet.WalletDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.jifenyuan.convert.wallet.WalletConvert;
import cn.iocoder.yudao.module.jifenyuan.dal.mysql.wallet.WalletMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.jifenyuan.enums.ErrorCodeConstants.*;

/**
 * 记分员 钱包 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class WalletServiceImpl implements WalletService {

    @Resource
    private WalletMapper walletMapper;

    @Override
    public Long createWallet(AppWalletCreateReqVO createReqVO) {
        // 插入
        WalletDO wallet = WalletConvert.INSTANCE.convert(createReqVO);
        walletMapper.insert(wallet);
        // 返回
        return wallet.getId();
    }

    @Override
    public void updateWallet(AppWalletUpdateReqVO updateReqVO) {
        // 校验存在
        validateWalletExists(updateReqVO.getId());
        // 更新
        WalletDO updateObj = WalletConvert.INSTANCE.convert(updateReqVO);
        walletMapper.updateById(updateObj);
    }

    @Override
    public int addWalletCoin(Long userId, Long coin) {
return walletMapper.addWalletCoin(userId,coin);
    }
    @Override
    public int addWalletVip(Long userId, int day){
return walletMapper.addWalletVip(userId,day);
    }

    @Override
    public int reduceWalletCoin(Long userId, Long coin) {
        return walletMapper.reduceWalletCoin(userId,coin);
    }

    @Override
    public void deleteWallet(Long id) {
        // 校验存在
        validateWalletExists(id);
        // 删除
        walletMapper.deleteById(id);
    }

    private void validateWalletExists(Long id) {
        if (walletMapper.selectById(id) == null) {
            throw exception(WALLET_NOT_EXISTS);
        }
    }

    @Override
    public WalletDO getWallet(Long id) {
        return walletMapper.selectById(id);
    }

    @Override
    public WalletDO getWalletByUserId(Long userId) {
        return walletMapper.selectWalletByUserId(userId);
    }

    @Override
    public List<WalletDO> getWalletList(Collection<Long> ids) {
        return walletMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<WalletDO> getWalletPage(AppWalletPageReqVO pageReqVO) {
        return walletMapper.selectPage(pageReqVO);
    }

    @Override
    public List<WalletDO> getWalletList(AppWalletExportReqVO exportReqVO) {
        return walletMapper.selectList(exportReqVO);
    }

}
